/**
 * Copyright 2013-2022 the original author or authors from the Jeddict project (https://jeddict.github.io/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.jeddict.settings.diagram;

import io.github.jeddict.settings.view.AttributeViewAs;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;
import org.netbeans.modeler.properties.spec.ComboBoxValue;
import org.openide.util.NbPreferences;

public final class ClassDiagramSettings extends javax.swing.JPanel {

    ClassDiagramSettings() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        rootLayeredPane = new javax.swing.JLayeredPane();
        refractorNamedQueryComp = new javax.swing.JCheckBox();
        deleteNamedQueryComp = new javax.swing.JCheckBox();
        javaseWrapperPanel = new javax.swing.JLayeredPane();
        javaseSupportComp = new javax.swing.JCheckBox();
        javaseSupportLabel = new javax.swing.JLabel();
        optionalReturnTypePanel = new javax.swing.JLayeredPane();
        optionalReturnTypeStatusLabel = new javax.swing.JLabel();
        optionalReturnTypeStatusComp = new javax.swing.JComboBox<>();
        compositePKWrapperPanel = new javax.swing.JLayeredPane();
        compositePrimaryKeyTypeLabel = new javax.swing.JLabel();
        defaultCompositePrimaryKeyTypeComp = new javax.swing.JComboBox<>();
        defaultSingleAssociationFetchTypeWrapperPanel = new javax.swing.JLayeredPane();
        defaultSingleAssociationFetchTypeLabel = new javax.swing.JLabel();
        defaultSingleAssociationFetchTypeComp = new javax.swing.JComboBox<>();
        dataTypeLayeredPane = new javax.swing.JLayeredPane();
        dataTypeLabel = new javax.swing.JLabel();
        dataTypeComboBox = new javax.swing.JComboBox();

        rootLayeredPane.setLayout(new java.awt.GridLayout(9, 1, 0, 2));

        org.openide.awt.Mnemonics.setLocalizedText(refractorNamedQueryComp, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.refractorNamedQueryComp.text")); // NOI18N
        rootLayeredPane.add(refractorNamedQueryComp);

        org.openide.awt.Mnemonics.setLocalizedText(deleteNamedQueryComp, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.deleteNamedQueryComp.text")); // NOI18N
        rootLayeredPane.add(deleteNamedQueryComp);

        org.openide.awt.Mnemonics.setLocalizedText(javaseSupportComp, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.javaseSupportComp.text")); // NOI18N

        javaseSupportLabel.setForeground(new java.awt.Color(153, 153, 153));
        org.openide.awt.Mnemonics.setLocalizedText(javaseSupportLabel, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.javaseSupportLabel.text")); // NOI18N

        javaseWrapperPanel.setLayer(javaseSupportComp, javax.swing.JLayeredPane.DEFAULT_LAYER);
        javaseWrapperPanel.setLayer(javaseSupportLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout javaseWrapperPanelLayout = new javax.swing.GroupLayout(javaseWrapperPanel);
        javaseWrapperPanel.setLayout(javaseWrapperPanelLayout);
        javaseWrapperPanelLayout.setHorizontalGroup(
            javaseWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javaseWrapperPanelLayout.createSequentialGroup()
                .addComponent(javaseSupportComp, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addComponent(javaseSupportLabel)
                .addGap(0, 288, Short.MAX_VALUE))
        );
        javaseWrapperPanelLayout.setVerticalGroup(
            javaseWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(javaseSupportComp, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
            .addComponent(javaseSupportLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        rootLayeredPane.add(javaseWrapperPanel);

        org.openide.awt.Mnemonics.setLocalizedText(optionalReturnTypeStatusLabel, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.optionalReturnTypeStatusLabel.text")); // NOI18N

        optionalReturnTypeStatusComp.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Disable", "Enable" }));

        optionalReturnTypePanel.setLayer(optionalReturnTypeStatusLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        optionalReturnTypePanel.setLayer(optionalReturnTypeStatusComp, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout optionalReturnTypePanelLayout = new javax.swing.GroupLayout(optionalReturnTypePanel);
        optionalReturnTypePanel.setLayout(optionalReturnTypePanelLayout);
        optionalReturnTypePanelLayout.setHorizontalGroup(
            optionalReturnTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionalReturnTypePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(optionalReturnTypeStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(optionalReturnTypeStatusComp, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        optionalReturnTypePanelLayout.setVerticalGroup(
            optionalReturnTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(optionalReturnTypePanelLayout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(optionalReturnTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(optionalReturnTypeStatusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addComponent(optionalReturnTypeStatusComp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        rootLayeredPane.add(optionalReturnTypePanel);

        org.openide.awt.Mnemonics.setLocalizedText(compositePrimaryKeyTypeLabel, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.compositePrimaryKeyTypeLabel.text")); // NOI18N

        defaultCompositePrimaryKeyTypeComp.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "IdClass", "EmbeddedId" }));

        compositePKWrapperPanel.setLayer(compositePrimaryKeyTypeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        compositePKWrapperPanel.setLayer(defaultCompositePrimaryKeyTypeComp, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout compositePKWrapperPanelLayout = new javax.swing.GroupLayout(compositePKWrapperPanel);
        compositePKWrapperPanel.setLayout(compositePKWrapperPanelLayout);
        compositePKWrapperPanelLayout.setHorizontalGroup(
            compositePKWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(compositePKWrapperPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(compositePrimaryKeyTypeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(defaultCompositePrimaryKeyTypeComp, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        compositePKWrapperPanelLayout.setVerticalGroup(
            compositePKWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(compositePKWrapperPanelLayout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(compositePKWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(compositePrimaryKeyTypeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addComponent(defaultCompositePrimaryKeyTypeComp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        rootLayeredPane.add(compositePKWrapperPanel);

        org.openide.awt.Mnemonics.setLocalizedText(defaultSingleAssociationFetchTypeLabel, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.defaultSingleAssociationFetchTypeLabel.text")); // NOI18N

        defaultSingleAssociationFetchTypeComp.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "EAGER", "LAZY" }));

        defaultSingleAssociationFetchTypeWrapperPanel.setLayer(defaultSingleAssociationFetchTypeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        defaultSingleAssociationFetchTypeWrapperPanel.setLayer(defaultSingleAssociationFetchTypeComp, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout defaultSingleAssociationFetchTypeWrapperPanelLayout = new javax.swing.GroupLayout(defaultSingleAssociationFetchTypeWrapperPanel);
        defaultSingleAssociationFetchTypeWrapperPanel.setLayout(defaultSingleAssociationFetchTypeWrapperPanelLayout);
        defaultSingleAssociationFetchTypeWrapperPanelLayout.setHorizontalGroup(
            defaultSingleAssociationFetchTypeWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(defaultSingleAssociationFetchTypeWrapperPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(defaultSingleAssociationFetchTypeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(defaultSingleAssociationFetchTypeComp, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        defaultSingleAssociationFetchTypeWrapperPanelLayout.setVerticalGroup(
            defaultSingleAssociationFetchTypeWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(defaultSingleAssociationFetchTypeWrapperPanelLayout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(defaultSingleAssociationFetchTypeWrapperPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(defaultSingleAssociationFetchTypeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                    .addComponent(defaultSingleAssociationFetchTypeComp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        rootLayeredPane.add(defaultSingleAssociationFetchTypeWrapperPanel);

        org.openide.awt.Mnemonics.setLocalizedText(dataTypeLabel, org.openide.util.NbBundle.getMessage(ClassDiagramSettings.class, "ClassDiagramSettings.dataTypeLabel.text")); // NOI18N
        dataTypeLabel.setMinimumSize(new java.awt.Dimension(90, 17));
        dataTypeLabel.setPreferredSize(new java.awt.Dimension(100, 17));

        List<ComboBoxValue> values = new ArrayList<>();
        for (AttributeViewAs value : AttributeViewAs.values()) {
            values.add(new ComboBoxValue(value, value.getDescription()));
        }
        dataTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel(values.toArray()));

        dataTypeLayeredPane.setLayer(dataTypeLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);
        dataTypeLayeredPane.setLayer(dataTypeComboBox, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout dataTypeLayeredPaneLayout = new javax.swing.GroupLayout(dataTypeLayeredPane);
        dataTypeLayeredPane.setLayout(dataTypeLayeredPaneLayout);
        dataTypeLayeredPaneLayout.setHorizontalGroup(
            dataTypeLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dataTypeLayeredPaneLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dataTypeLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(dataTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 225, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(88, Short.MAX_VALUE))
        );
        dataTypeLayeredPaneLayout.setVerticalGroup(
            dataTypeLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dataTypeLayeredPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(dataTypeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
                .addComponent(dataTypeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        rootLayeredPane.add(dataTypeLayeredPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rootLayeredPane)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(rootLayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    void load() {
        refractorNamedQueryComp.setSelected(isRefractorQuery());
        deleteNamedQueryComp.setSelected(isDeleteQuery());
        javaseSupportComp.setSelected(isJavaSESupportEnable());
        optionalReturnTypeStatusComp.setSelectedItem(isOptionalReturnType() ? "Enable" : "Disable");
        defaultCompositePrimaryKeyTypeComp.setSelectedItem(getDefaultCompositePrimaryKeyType());
        defaultSingleAssociationFetchTypeComp.setSelectedItem(getDefaultSingleAssociationFetchType());
        dataTypeComboBox.setSelectedItem(getDataType());
    }

    void store() {
        pref.putBoolean("refractorNamedQuery", refractorNamedQueryComp.isSelected());
        pref.putBoolean("deleteNamedQuery", deleteNamedQueryComp.isSelected());
        pref.putBoolean("javaSESupportEnable", javaseSupportComp.isSelected());
        pref.putBoolean("optionalReturnType", "Enable".equals(optionalReturnTypeStatusComp.getSelectedItem()));
        pref.put("defaultCompositePrimaryKeyType", (String) defaultCompositePrimaryKeyTypeComp.getSelectedItem());
        pref.put("defaultSingleAssociationFetchType", (String) defaultSingleAssociationFetchTypeComp.getSelectedItem());
        pref.put("dataType", ((AttributeViewAs) ((ComboBoxValue) dataTypeComboBox.getSelectedItem()).getValue()).name());
//      pref.put("dbArtifactCase", ((DBArtifactCase)((ComboBoxValue)dbArtifactCaseComboBox.getSelectedItem()).getValue()).name());

        deleteNamedQuery = null;
        refractorNamedQuery = null;
        javaSESupportEnable = null;
        optionalReturnTypeStatus = null;
        defaultCompositePrimaryKeyType = null;
        defaultSingleAssociationFetchType = null;
        attributeViewAs = null;

    }

    private static Boolean deleteNamedQuery;
    private static Boolean refractorNamedQuery;
    private static Boolean javaSESupportEnable;
    private static Boolean optionalReturnTypeStatus;
    private static String defaultCompositePrimaryKeyType;
    private static String defaultSingleAssociationFetchType;
    private static AttributeViewAs attributeViewAs;
//    private static DBArtifactCase dbArtifactCase;

    public static boolean isRefractorQuery() {
        if (refractorNamedQuery == null) {
            refractorNamedQuery = pref.getBoolean("refractorNamedQuery", Boolean.TRUE);
        }
        return refractorNamedQuery;
    }

    public static boolean isJavaSESupportEnable() {
        if (javaSESupportEnable == null) {
            javaSESupportEnable = pref.getBoolean("javaSESupportEnable", Boolean.FALSE);
        }
        return javaSESupportEnable;
    }

    public static boolean isOptionalReturnType() {
        if (optionalReturnTypeStatus == null) {
            optionalReturnTypeStatus = pref.getBoolean("optionalReturnType", Boolean.FALSE);
        }
        return optionalReturnTypeStatus;
    }

    public static boolean isDeleteQuery() {
        if (deleteNamedQuery == null) {
            deleteNamedQuery = pref.getBoolean("deleteNamedQuery", Boolean.FALSE);
        }
        return deleteNamedQuery;
    }

    public static String getDefaultCompositePrimaryKeyType() {
        if (defaultCompositePrimaryKeyType == null) {
            defaultCompositePrimaryKeyType = pref.get("defaultCompositePrimaryKeyType", "IdClass");
        }
        return defaultCompositePrimaryKeyType;
    }

    public static boolean isEmbeddedIdDefaultType() {
        return "EmbeddedId".equals(getDefaultCompositePrimaryKeyType());
    }

    public static String getDefaultSingleAssociationFetchType() {
        if (defaultSingleAssociationFetchType == null) {
            defaultSingleAssociationFetchType = pref.get("defaultSingleAssociationFetchType", "EAGER");
        }
        return defaultSingleAssociationFetchType;
    }

    public static boolean isLazyDefaultTypeForSingleAssociation() {
        return "LAZY".equals(getDefaultSingleAssociationFetchType());
    }

    public static AttributeViewAs getDataType() {
        if (attributeViewAs == null) {
            attributeViewAs = AttributeViewAs.valueOf(pref.get("dataType", AttributeViewAs.NONE.name()));
        }
        return attributeViewAs;
    }
    
//    public static DBArtifactCase getDBArtifactCase() {
//        if (dbArtifactCase == null) {
//            dbArtifactCase = DBArtifactCase.valueOf(NbPreferences.forModule(ViewPanel.class).get("dbArtifactCase", DBArtifactCase.UPPER_CASE.name()));
//        }
//        return dbArtifactCase;
//    }
    public boolean valid() {
        return true;
    }

    private static final Preferences pref = NbPreferences.forModule(ClassDiagramSettings.class);
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLayeredPane compositePKWrapperPanel;
    private javax.swing.JLabel compositePrimaryKeyTypeLabel;
    private javax.swing.JComboBox dataTypeComboBox;
    private javax.swing.JLabel dataTypeLabel;
    private javax.swing.JLayeredPane dataTypeLayeredPane;
    private javax.swing.JComboBox<String> defaultCompositePrimaryKeyTypeComp;
    private javax.swing.JComboBox<String> defaultSingleAssociationFetchTypeComp;
    private javax.swing.JLabel defaultSingleAssociationFetchTypeLabel;
    private javax.swing.JLayeredPane defaultSingleAssociationFetchTypeWrapperPanel;
    private javax.swing.JCheckBox deleteNamedQueryComp;
    private javax.swing.JCheckBox javaseSupportComp;
    private javax.swing.JLabel javaseSupportLabel;
    private javax.swing.JLayeredPane javaseWrapperPanel;
    private javax.swing.JLayeredPane optionalReturnTypePanel;
    private javax.swing.JComboBox<String> optionalReturnTypeStatusComp;
    private javax.swing.JLabel optionalReturnTypeStatusLabel;
    private javax.swing.JCheckBox refractorNamedQueryComp;
    private javax.swing.JLayeredPane rootLayeredPane;
    // End of variables declaration//GEN-END:variables
}
